package ar.com.jwa.controllers;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import ar.com.jwa.commons.validators.LoginValidator;
import ar.com.jwa.domain.Game;
import ar.com.jwa.domain.User;
import ar.com.jwa.services.SystemFacade;

@Controller
public class UserController {
	
	private Log log = LogFactory.getLog(UserController.class); 
	
	@Autowired
	private SystemFacade systemFacade;
	public static final String DATE_FORMAT = "dd-MM HH:mm";

	@RequestMapping("/login.*")
	public ModelAndView login(@ModelAttribute("user") User user,BindingResult bindingResult,HttpServletRequest req, HttpServletResponse res,HttpSession session) {
		
		LoginValidator lv= new LoginValidator(); 
		
		lv.validate(user, bindingResult);
		
		ModelAndView mav = new ModelAndView();
		
		if (bindingResult.hasErrors()) {
			mav.getModel().putAll(bindingResult.getModel());
			mav.setViewName("login");
			return mav;
		}
		
		boolean logged = this.getSystemFacade().login(user);
		
		if (logged){
			
			req.getSession().setAttribute("User", user);
//			Boolean hasToConfirm = this.getSystemFacade().hasToConfirmSomething(user);
			Collection<Game> games = this.getSystemFacade().getGamesToConfirm();
			
			if (games.size()!= 0){
				mav.addObject("gameId",games.iterator().next().getId());
				mav.addObject("hasToConfirm",games.size() != 0);
			}
			
			mav.addObject("hasToConfirm",games.size() != 0);
			mav.addObject("theDate",new SimpleDateFormat(DATE_FORMAT).format(Calendar.getInstance().getTime()));
			mav.setViewName("home");
			
		}
		else {
			mav.setViewName("login");
			mav.getModel().putAll(bindingResult.getModel());
		}
		
		return mav;
	}
	
	@RequestMapping("/setup.*")
	public ModelAndView setupForm(HttpServletRequest req, HttpServletResponse res,HttpSession session){
		ModelAndView mav = new ModelAndView("login");
		return mav;
	}
	
	@RequestMapping("/confirm.do")
	public void confirmGame(HttpSession session){
		
	}
	
	/**
	 * @return the systemFacade
	 */
	public SystemFacade getSystemFacade() {
		if (this.systemFacade == null)
			throw new RuntimeException("SystemFacade is not setted up");
		return systemFacade;
	}

	/**
	 * @param systemFacade the systemFacade to set
	 */
	public void setSystemFacade(SystemFacade systemFacade) {
		this.systemFacade = systemFacade;
	}

	/**
	 * @return the log
	 */
	public Log getLog() {
		return log;
	}

	/**
	 * @param log the log to set
	 */
	public void setLog(Log log) {
		this.log = log;
	}

}
